package com.apexedu.portal.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.portal.action.PortalFooterAction;
import com.apexedu.portal.entity.TFooter;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by janabo on 2014-10-11.
 */
@Service("portalFooterService")
public class PortalFooterServiceImpl extends AbstractBaseServiceSupport {

    @SuppressWarnings("unchecked")
    @Override
    public List<TFooter> list(Object myaction, Pager pager) throws Exception {
        List<TFooter> list = DBUtil.queryAllBeanList("select * from T_FOOTER t", TFooter.class);
        return list;
    }

    @Override
    public void saveOrUpdate(Object myaction) throws Exception {
        PortalFooterAction action = (PortalFooterAction) myaction;
        TFooter footer = action.getFooter();
        if (footer != null) {
            if ("1".equals(footer.getState())) {//只能有一个状态为1的
                List<TFooter> list = DBUtil.queryAllBeanList("select * from T_FOOTER t where t.state='1'", TFooter.class);
                for (TFooter f : list) {
                    f.setState("0");
                }
                baseDao.saveOrUpdateAll(list);
            }

            if (StringUtil.isNotBlank(footer.getWid())) {
                baseDao.updateNotNull(footer);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_FOOTER, Const.LOG_ACTION_UPDATE, IdLoginAction.getIpAddr(action.getRequest()));
            } else {
                footer.setWid(SeqFactory.getNewSequenceAlone());
                baseDao.save(footer);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_FOOTER, Const.LOG_ACTION_SAVE, IdLoginAction.getIpAddr(action.getRequest()));
            }
        }
    }

    @Override
    public void load(Object myaction) throws Exception {
        PortalFooterAction action = (PortalFooterAction) myaction;
        String wid = action.getWid();
        TFooter footer = getBaseDao().findById(TFooter.class, wid);
        action.setFooter(footer);
    }

    @Override
    public boolean remove(Object myaction) throws Exception {
        PortalFooterAction action = (PortalFooterAction) myaction;
        String ids = action.getWid();
        boolean deleteSuccess = getBaseDao().deleteAll("TFooter", "wid", "=", ids);
        if (deleteSuccess)
            BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_FOOTER, Const.LOG_ACTION_DEL,
                    IdLoginAction.getIpAddr(action.getRequest()));
        return deleteSuccess;
    }
}
